#include "StdAfx.h"

 
#include "qaerowindow.h"
using namespace micro::gui;


#include "qdebug.h"
using namespace micro::utils;



QAeroWindow::QAeroWindow(void)
{
	m_isAero = IsAeroEnabled();
	MARGINS m = {0};
	m_margins = m;



	//NONCLIENTMETRICS ncm = { sizeof(NONCLIENTMETRICS) };
	//SystemParametersInfo ( SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &ncm, false );
	//lf = ncm.lfMessageFont;

}


QAeroWindow::~QAeroWindow()
{
	 
}


bool QAeroWindow::IsAeroEnabled() const
{
	HRESULT hr;
	BOOL bEnabled;

    hr = ::DwmIsCompositionEnabled ( &bEnabled );
    return SUCCEEDED(hr) && bEnabled;
}




HRESULT QAeroWindow::EnableBlurBehindWindow(  bool enable, HRGN region, bool transitionOnMaximized  )
{
    DWM_BLURBEHIND blurBehind = { 0 };
        
    blurBehind.dwFlags = DWM_BB_ENABLE | DWM_BB_TRANSITIONONMAXIMIZED;
    blurBehind.fEnable = enable;
    blurBehind.fTransitionOnMaximized = transitionOnMaximized;

    if (enable && 0 != region)
    {
        blurBehind.dwFlags |= DWM_BB_BLURREGION;
        blurBehind.hRgnBlur = region;
    }

    return ::DwmEnableBlurBehindWindow(_window, &blurBehind);
}

//template <typename T>
void QAeroWindow::DwmSetWindowAttribute( const DWMWINDOWATTRIBUTE attribute,   LPCVOID   value, int cb)
{
    ASSERT(0 != _window);
    ::DwmSetWindowAttribute(_window, attribute, value, cb );
}

//void QAero::DwmSetWindowAttribute(  const DWMWINDOWATTRIBUTE attribute, bool value)
//{
//    ASSERT(0 != _window);
//
//    ::DwmSetWindowAttribute(_window,
//                            attribute,
//                            static_cast<BOOL>(value));
//}



HRESULT QAeroWindow::ExtendIntoClient( )
{ 
		//MARGINS margins = {0, 0, 100, 100}; //LEFT RIGHT TOP BOTTOM
		MARGINS margins =  {-1 }; //all
		HRESULT hr = S_OK;
		 
		hr = ::DwmExtendFrameIntoClientArea( _window, &margins);		 
		return hr;
}

HRESULT QAeroWindow::ExtendFrame(bool extend)
{
    ASSERT(0 != _window);
	HRESULT hr = S_OK;


    if (m_isAero)
    {
        const MARGINS dontExtend = { 0 };

        if (extend)
        {
           hr =  ::DwmExtendFrameIntoClientArea(_window, &m_margins);
        }
        else
        {
            hr = ::DwmExtendFrameIntoClientArea(_window,  &dontExtend);
        }
    }

	return hr;
}


//bool QAero::IsCompositionEnabled()
//{
//    HMODULE library = ::LoadLibrary(L"dwmapi.dll");
//    bool result = false;
//
//    if (0 != library)
//    {
//        if (0 != ::GetProcAddress(library, 
//                                    "DwmIsCompositionEnabled"))
//        {
//            BOOL enabled = FALSE;
//            result = SUCCEEDED(::DwmIsCompositionEnabled(&enabled)) && enabled;
//        }
//
//        ::FreeLibrary(library) ;
//    }
//
//    return result;
//}